﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ProgNetComponentsDx.Browsers;
using System.Data;
using System.ComponentModel;
using System.Drawing.Design;

namespace ProgNetComponentsDx.Validation.Rules
{
    public abstract class BrowserValidationRule : ProgNetComponents.Validation.Rules.ColumnValidationRule
    {
        #region Members
        private Browser _Browser;
        #endregion

        #region Properties
        /// <summary>
        /// Getst browser base on BrowserId
        /// </summary>
        protected Browser Browser
        {
            get
            {
                if (_Browser == null || !BrowserCache.IsTheSameBrowser(_Browser.BrowserId, BrowserId))
                {
                    _Browser = BrowserCache.GetBrowser(BrowserId, false);
                }
                return _Browser;
            }
        }

        /// <summary>
        /// Getst or sets Browser Id
        /// </summary>
        [Editor(typeof(ProgNetComponentsDx.Browsers.Design.Editors.BrowserNameEditor), typeof(UITypeEditor))]
        public object BrowserId
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets if null value is valid
        /// </summary>
        public bool IsNullValid
        {
            get;
            set;
        }

        #endregion

        protected BrowserValidationRule(string column, object browserId, string error = null):base(column, error)
        {
            BrowserId = browserId;
            IsNullValid = true;
        }
        protected BrowserValidationRule() { }
        protected bool IsValueValid(DataRow row)
        {
            return Browser.IsValid(IsNullValid, row[Column]);
        }
    }
}
